package edu.chl.dat076.datesite.controllers;

import edu.chl.dat076.datesite.ejbs.ConversationEJBLocal;
import edu.chl.dat076.datesite.ejbs.UserEJBLocal;
import edu.chl.dat076.datesite.entities.Conversation;
import edu.chl.dat076.datesite.entities.Message;
import edu.chl.dat076.datesite.entities.SiteUser;
import java.io.Serializable;
import java.util.Calendar;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Christoffer
 */
@ManagedBean
@ViewScoped
public class MessageControllerBean implements Serializable {
    
    @EJB
	private UserEJBLocal userEJBLocal;
	@EJB
	private ConversationEJBLocal conversationEJBLocal;
	
    private SiteUser user;
	private SiteUser otherUser;
	private Conversation conversation;
	private Message message = new Message();
	
	@PostConstruct
	public void postConstruct() {
		String mFromSite = FacesContext.getCurrentInstance().getExternalContext().getFlash().get("mFromSite").toString();
		user = userEJBLocal.getUser(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user").toString());
		
		// Konversationen och den andra användaren i konversationen hanteras olika beroede på
		// varifrån användaren har navigerat in till meddelandena
		if (mFromSite.equals("profile")) {
			otherUser = userEJBLocal.getUser(FacesContext.getCurrentInstance().getExternalContext().getFlash().get("receiverUser").toString());
			conversation = conversationEJBLocal.getConversation(user, otherUser);
		} else if (mFromSite.equals("conversations")) {
			conversation = (Conversation) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("conversation");
			otherUser = (conversation.getUser1().equals(user)) ? conversation.getUser2() : conversation.getUser1();
		}
	}
	
	@PreDestroy
	public void preDestroy() {
		setRead();
	}

    public String postMessage() {
		// Skapa ny konversationen om ingen finns, annars lägg till meddelandet
        if (conversation == null) {
			message.setRead(false);
			message.setSender(user);
			message.setSent(Calendar.getInstance().getTime());
			conversation = conversationEJBLocal.newConversation(user, otherUser, message);
		} else {
			message.setRead(false);
			message.setSender(user);
			message.setSent(Calendar.getInstance().getTime());
			conversationEJBLocal.addMessage(conversation, message);
		}
		
		FacesContext.getCurrentInstance().getExternalContext().getFlash().put("mFromSite", "conversations");
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("conversation", conversation);
        return "conversation.jsf?faces-redirect=true";
    }
	
	// Sätt alla olästa meddelanden till lästa
	public void setRead() {
		for (Message m : conversation.getMessages()) {
			if (!m.getSender().equals(user)) {
				if(!m.isRead()) {
					m.setRead(true);
					conversationEJBLocal.updateMessage(m);
				}
			}
		}
	}
	
	public Message getMessage() {
		return message;
	}
	
	public Conversation getConversation () {
        return conversation;
    }

	public SiteUser getOtherUser() {
		return otherUser;
	}
	
	public void setMessage(Message message) {
		this.message = message;
	}
}
